#include <FastLED.h>
#include <WiFi.h>
#include <WiFiClient.h>
#include <NTPClient.h>
const char *ntpServer = "cn.pool.ntp.org";
const long gmtOffset_sec = 8 * 3600;  // GMT offset in seconds (+8 hours for China Standard Time)
const int daylightOffset_sec = 0;     // No daylight offset for China Standard Time

#define LED_PIN     7     // WS2812B灯带数据线连接到Arduino的6号引脚
#define NUM_LEDS    28    // 灯带上LED的数量
#define LED_TYPE    WS2812B
#define COLOR_ORDER GRB

#define BULLET_SIZE 5     // 子弹的长度
#define BULLET_SPEED 60    // 子弹的移动速度，数值越小速度越快

CRGB leds[NUM_LEDS];

// 创建一个NTP客户端实例
WiFiUDP ntpUDP;
NTPClient timeClient(ntpUDP, ntpServer, gmtOffset_sec);
char formattedTime[20]; // Store formatted time

int hour;
int minute;

const char *ssid = "1708";
const char *password = "12345677";


void setup() {
  Serial.begin(115200);
  delay(1000);

  // 连接到 WiFi
  Serial.println("Connecting to WiFi");
  WiFi.begin(ssid, password);
  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");
  }

  Serial.println("WiFi连接成功, IP地址: ");
  Serial.println(WiFi.localIP());
  

  delay(2000);
  // 启动NTP客户端
  timeClient.begin();

  FastLED.addLeds<LED_TYPE, LED_PIN, COLOR_ORDER>(leds, NUM_LEDS).setCorrection(TypicalLEDStrip);
  FastLED.setBrightness(10);  // 设置亮度，范围为0-255

  // configTime(8 * 3600, 0, NTP1, NTP2, NTP3);

  // fill_solid(leds, NUM_LEDS, CRGB::White);
  // FastLED.show();

  /// 设置小时
  // if(hour == 1) {
  //   fill_solid(leds, NUM_LEDS, CRGB::Black);
  //   leds[14] = CRGB::White;
  //   leds[15] = CRGB::White;
  //   leds[22] = CRGB::White;
  //   leds[23] = CRGB::White;

  //   leds[11] = CRGB::White;
  //   leds[10] = CRGB::White;
  //   leds[9] = CRGB::White;
  //   leds[8] = CRGB::White;
  //   leds[7] = CRGB::White;
  //   leds[6] = CRGB::White;
  //   leds[5] = CRGB::White;
  //   leds[4] = CRGB::White;
  //   leds[3] = CRGB::White;
  //   leds[2] = CRGB::White;
  //   // fill_solid(leds, NUM_LEDS, CHSV(random(256), 255, 255));
  //   FastLED.show();
  // }
}

//time_t now; //实例化时间
void setClock()
{

  timeClient.update(); // 更新NTP时间
 
  // 获取当前时间
  unsigned long epochTime = timeClient.getEpochTime();
 
  // Print formatted time
  struct tm *timeinfo;
  timeinfo = localtime((const time_t *)&epochTime);

  // Format time (HH:MM:SS)
  strftime(formattedTime, sizeof(formattedTime), "%H:%M:%S", timeinfo);
  // Print formatted time
  Serial.print("Formatted time (HH:MM:SS): ");
  Serial.println(formattedTime);
  sscanf(formattedTime, "%d:", &hour);
  // Print hour
  Serial.print("小时: ");
  Serial.println(hour);
  
  sscanf(formattedTime, "%*d:%d:", &minute);
  // Print minute
  Serial.print("分钟: ");
  Serial.println(minute);
  setMinLed();
}

void loop() {
  // static uint8_t startPos = 0;  // 子弹起始位置

  // fill_solid(leds, NUM_LEDS, CRGB::Black);
  // leds[random(14)] = CHSV(random(256), 255, 255);
  // leds[random(14)] = CHSV(random(256), 255, 255);
  // leds[random(14)] = CHSV(random(256), 255, 255);
  // // fill_solid(leds, NUM_LEDS, CHSV(random(256), 255, 255));
  // FastLED.show();
  // delay(1000);

  // 清空LED
  // fill_solid(leds, NUM_LEDS, CRGB::Black);
  // // fill_solid(leds, NUM_LEDS, CHSV(random(256), 255, 255));
  // // FastLED.show();
  // // delay(500);
  // // 绘制子弹
  // for (int i = 0; i < BULLET_SIZE; i++) {
  //   int pos = startPos + i;
  //   if (pos < NUM_LEDS) {
  //     // 为每个子弹生成随机颜色
  //     leds[pos] = CHSV(random(256), 255, 255);
  //   }
  // }

  // // 更新子弹位置
  // startPos = (startPos + 1) % NUM_LEDS;
  // Serial.print(startPos);
  // // 显示效果
  // FastLED.show();

  // // 延迟一段时间以控制子弹的移动速度
  // delay(BULLET_SPEED);


  
  delay(5000);
  setClock();
}

void setHourLed(){
  if(hour > 9) {
    String hourString = String(hour);
    String hour1 = hourString.substring(0,1);
    String hour2 = hourString.substring(1);
    fill_solid(leds, NUM_LEDS, CRGB::Black);
    if (hour1 == "1") {
      int hourLeft[] = {14,15,22,23};
      int arrayLength = sizeof(hourLeft) / sizeof(hourLeft[0]); // 计算数组的长度
      for (int i = 0; i < arrayLength; i++) {
        leds[hourLeft[i]] = CRGB::White;
      }
    } else if(hour1 == "2") {
      int hourLeft[] = {16,17,18,19,20,21,22,23,24,25};
    }

    if (hour2 == "0") {
      int hourRight[] = {0,1,2,3,4,5,8,9,10,11,12,13};
      int arrayLength = sizeof(hourRight) / sizeof(hourRight[0]); // 计算数组的长度
      for (int i = 0; i < arrayLength; i++) {
        leds[hourRight[i]] = CRGB::White;
      }
    } else if (hour2 == "1") {
      int hourRight[] = {0,1,8,9};
      int arrayLength = sizeof(hourRight) / sizeof(hourRight[0]); // 计算数组的长度
      for (int i = 0; i < arrayLength; i++) {
        leds[hourRight[i]] = CRGB::White;
      }
    } else if (hour2 == "2") {
      int hourRight[] = {2,3,4,5,6,7,8,9,10,11};
      int arrayLength = sizeof(hourRight) / sizeof(hourRight[0]); // 计算数组的长度
      for (int i = 0; i < arrayLength; i++) {
        leds[hourRight[i]] = CRGB::White;
      }
    } else if (hour2 == "3") {
      int hourRight[] = {0,1,2,3,6,7,8,9,10,11};
      int arrayLength = sizeof(hourRight) / sizeof(hourRight[0]); // 计算数组的长度
      for (int i = 0; i < arrayLength; i++) {
        leds[hourRight[i]] = CRGB::White;
      }
    } else if (hour2 == "4") {
      int hourRight[] = {0,1,6,7,8,9,12,13};
      int arrayLength = sizeof(hourRight) / sizeof(hourRight[0]); // 计算数组的长度
      for (int i = 0; i < arrayLength; i++) {
        leds[hourRight[i]] = CRGB::White;
      }
    } else if (hour2 == "5") {
      int hourRight[] = {0,1,2,3,6,7,10,11,12,13};
      int arrayLength = sizeof(hourRight) / sizeof(hourRight[0]); // 计算数组的长度
      for (int i = 0; i < arrayLength; i++) {
        leds[hourRight[i]] = CRGB::White;
      }
    } else if (hour2 == "6") {
      int hourRight[] = {0,1,2,3,4,5,6,7,10,11,12,13};
      int arrayLength = sizeof(hourRight) / sizeof(hourRight[0]); // 计算数组的长度
      for (int i = 0; i < arrayLength; i++) {
        leds[hourRight[i]] = CRGB::White;
      }
    } else if (hour2 == "7") {
      int hourRight[] = {0,1,8,9,10,11};
      int arrayLength = sizeof(hourRight) / sizeof(hourRight[0]); // 计算数组的长度
      for (int i = 0; i < arrayLength; i++) {
        leds[hourRight[i]] = CRGB::White;
      }
    } else if (hour2 == "8") {
      int hourRight[] = {0,1,2,3,4,5,6,7,8,9,10,11,12,13};
      int arrayLength = sizeof(hourRight) / sizeof(hourRight[0]); // 计算数组的长度
      for (int i = 0; i < arrayLength; i++) {
        leds[hourRight[i]] = CRGB::White;
      }
    } else if (hour2 == "9") {
      int hourRight[] = {0,1,2,3,6,7,8,9,10,11,12,13};
      int arrayLength = sizeof(hourRight) / sizeof(hourRight[0]); // 计算数组的长度
      for (int i = 0; i < arrayLength; i++) {
        leds[hourRight[i]] = CRGB::White;
      }
    }
    
    FastLED.show();
  }
}

void setMinLed(){
  fill_solid(leds, NUM_LEDS, CRGB::Black);
  String min2;
  if(minute < 10) {
    Serial.print("分钟小于10");
    int minLeft[] = {14,15,16,17,18,19,22,23,24,25,26,27};
    int arrayLength = sizeof(minLeft) / sizeof(minLeft[0]); // 计算数组的长度
    comLed(arrayLength, minLeft);
    min2 = String(minute);
  } else {
    String minString = String(minute);
    String min1 = minString.substring(0,1);
    min2 = minString.substring(1);
    if (min1 == "1") {
      int minLeft[] = {14,15,22,23};
      int arrayLength = sizeof(minLeft) / sizeof(minLeft[0]); // 计算数组的长度
      comLed(arrayLength, minLeft);
    } else if(min1 == "2") {
      int minLeft[] = {16,17,18,19,20,21,22,23,24,25};
      int arrayLength = sizeof(minLeft) / sizeof(minLeft[0]); // 计算数组的长度
      comLed(arrayLength, minLeft);
    } else if(min1 == "3") {
      int minLeft[] = {14,15,16,17,20,21,22,23,24,25};
      int arrayLength = sizeof(minLeft) / sizeof(minLeft[0]); // 计算数组的长度
      comLed(arrayLength, minLeft);
    } else if(min1 == "4") {
      int minLeft[] = {14,15,20,21,22,23,26,27};
      int arrayLength = sizeof(minLeft) / sizeof(minLeft[0]); // 计算数组的长度
      comLed(arrayLength, minLeft);
    } else if(min1 == "5") {
      int minLeft[] = {14,15,16,17,20,21,24,25,26,27};
      int arrayLength = sizeof(minLeft) / sizeof(minLeft[0]); // 计算数组的长度
      comLed(arrayLength, minLeft);
    }
  }
  if (min2 == "0") {
    int minRight[] = {0,1,2,3,4,5,8,9,10,11,12,13};
    int arrayLength = sizeof(minRight) / sizeof(minRight[0]); // 计算数组的长度
    comLed(arrayLength, minRight);
  } else if (min2 == "1") {
    int minRight[] = {0,1,8,9};
    int arrayLength = sizeof(minRight) / sizeof(minRight[0]); // 计算数组的长度
    comLed(arrayLength, minRight);
  } else if (min2 == "2") {
    int minRight[] = {2,3,4,5,6,7,8,9,10,11};
    int arrayLength = sizeof(minRight) / sizeof(minRight[0]); // 计算数组的长度
    comLed(arrayLength, minRight);
  } else if (min2 == "3") {
    int minRight[] = {0,1,2,3,6,7,8,9,10,11};
    int arrayLength = sizeof(minRight) / sizeof(minRight[0]); // 计算数组的长度
    comLed(arrayLength, minRight);
  } else if (min2 == "4") {
    int minRight[] = {0,1,6,7,8,9,12,13};
    int arrayLength = sizeof(minRight) / sizeof(minRight[0]); // 计算数组的长度
    comLed(arrayLength, minRight);
  } else if (min2 == "5") {
    int minRight[] = {0,1,2,3,6,7,10,11,12,13};
    int arrayLength = sizeof(minRight) / sizeof(minRight[0]); // 计算数组的长度
    comLed(arrayLength, minRight);
  } else if (min2 == "6") {
    int minRight[] = {0,1,2,3,4,5,6,7,10,11,12,13};
    int arrayLength = sizeof(minRight) / sizeof(minRight[0]); // 计算数组的长度
    comLed(arrayLength, minRight);
  } else if (min2 == "7") {
    int minRight[] = {0,1,8,9,10,11};
    int arrayLength = sizeof(minRight) / sizeof(minRight[0]); // 计算数组的长度
    comLed(arrayLength, minRight);
  } else if (min2 == "8") {
    int minRight[] = {0,1,2,3,4,5,6,7,8,9,10,11,12,13};
    int arrayLength = sizeof(minRight) / sizeof(minRight[0]); // 计算数组的长度
    comLed(arrayLength, minRight);
  } else if (min2 == "9") {
    int minRight[] = {0,1,2,3,6,7,8,9,10,11,12,13};
    int arrayLength = sizeof(minRight) / sizeof(minRight[0]); // 计算数组的长度
    comLed(arrayLength, minRight);
  }
  FastLED.show();
}

void comLed(int arrayLength, int array[]){
  for (int i = 0; i < arrayLength; i++) {
    leds[array[i]] = CRGB::White;
  }
}
